//
//  WalletDetailsViewController.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/6.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "WalletDetailsViewController.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "MessageTableViewCell.h"
#import "MJRefresh.h"

@interface WalletDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *mesTableView;
@property (nonatomic, assign)int currentpPage;

@property (nonatomic, strong)NSMutableArray *detailArray; //明细
@property (nonatomic, copy)NSString *putMoney;//收入
@property (nonatomic, copy)NSString *outMoney;//支出

@end

@implementation WalletDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.detailArray = [[NSMutableArray alloc]init];
    self.currentpPage = 1;
    
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    [self.titleBtn setTitle:@"明细" forState:UIControlStateNormal];
    
    self.mesTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height)];
    self.mesTableView.backgroundColor = [UIColor clearColor];
    self.mesTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mesTableView.delegate = self;
    self.mesTableView.dataSource = self;
    [self.view addSubview:self.mesTableView];

    
    self.mesTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.detailArray.count>0) {
              [self.detailArray removeAllObjects];
        }
        self.currentpPage = 1;
        [self requestDetail];
        
    }];
    
    self.mesTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.currentpPage==0) {
            [self.mesTableView.mj_footer endRefreshing];
        }else{
            [self requestDetail];
        }
    }];
    
    [self.mesTableView.mj_header beginRefreshing];
}


- (void)requestDetail{
    
    NSDictionary *parma = @{@"userToken":[BGMUser shareInstance].userToken,
                            @"userId":[BGMUser shareInstance].userId,
                            @"pageNum":[NSString stringWithFormat:@"%d",self.currentpPage],
                            @"pageSize":@"10"
                            };
    [BGMHTTP requestWithMethodName:@"users/getUserBillDetail" parameters:parma completion:^(BOOL success, NSError *error, NSDictionary *result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.mesTableView.mj_footer endRefreshing];
            [self.mesTableView.mj_header endRefreshing];
            [self userDeatil:[result objectForKey:@"data"]];
        });

    }];
    
}

- (void)userDeatil:(NSDictionary *)dic{
    NSLog(@"asdfsafa");
    NSArray *billList = [dic objectForKey:@"billList"];
    [self.detailArray addObjectsFromArray:billList];
    self.putMoney = [NSString stringWithFormat:@"%@",[dic objectForKey:@"putMoney"]];
    self.outMoney = [NSString stringWithFormat:@"%@",[dic objectForKey:@"outMoney"]];
    if (billList.count==10) {
        self.currentpPage = self.currentpPage + 1;
    }else{
        self.currentpPage = 0;
    }
    [self.mesTableView reloadData];

    
}
#pragma mark UITABLEVIEWDELEAGE

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.detailArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"MessageTableViewCell";
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell ==nil) {
        cell = [[MessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dic = [self.detailArray objectAtIndex:indexPath.row];
    [cell userDetailnfo:dic];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


@end
